$(document).ready(function () {
    $("#queryBtn").click(function () {

        $.ajax({
            type: "POST",
            url: "listConfig.do",
            async: false,
            dataType: 'json',
            success: function (configList) {
                $("#statusTbl tbody").remove();

                var cnt = configList.length;
                var str = "<tbody>"
                for (var i = 0; i < cnt; i++) {
                    str = str + "<tr>";
                    var config = configList[i];
                    str = str + "<td>" + config["tableName"] + "</td>";
                    str = str + "<td>" + config["pptFlg"] + "</td>";
                    str = str + "<td>" + config["pptTableSchema"] + "</td>";
                    str = str + "<td>" + config["pptTableName"] + "</td>";
                    str = str + "<td>" + config["hisFlg"] + "</td>";
                    str = str + "<td>" + config["hisTableSchema"] + "</td>";
                    str = str + "<td>" + config["hisTableName"] + "</td>";
                    str = str + "<td><button class='btn btn-danger'>删除</button></td>";
                    str = str + "</tr>";
                }
                $(str).appendTo($("#statusTbl"));

            }
        })
    });
    $("#statusTbl").on("click", "tbody tr button", function () {
        var tds = $(this).parent().parent().find("td");
        var tableName = $(tds[0]).text();
        // $("#statusTbl").showCallBackWarnningDialog({
        //     errMsg: "是否删除删除配置,请确认!!!!",
        //     callbackFn: function (data) {
        //         if (data.result == true) {
        $.ajax({
            type: "POST",
            url: "deleteConfig.do",
            async: false,
            // dataType: 'json',
            data: {tableName: tableName},
            success: function (config) {
                console.info(config);
                showMessengerSuccessDialog("删除成功", 2);
                $("#queryBtn").click();
            },
            error: function (e, f) {
                console.info(e);
                console.info(f);
            }
        });
        //         }
        //     }
        // });

    });

    $("#statusTbl").on("click", "tbody tr:not(button)", function () {
        var tableName = $(this).find("td:eq(0)").text();
        var pptFlg = $(this).find("td:eq(1)").text();
        var pptTableSchema = $(this).find("td:eq(2)").text();
        var pptTableName = $(this).find("td:eq(3)").text();
        var hisFlg = $(this).find("td:eq(4)").text();
        var hisTableSchema = $(this).find("td:eq(5)").text();
        var hisTableName = $(this).find("td:eq(6)").text();

        $("#tableNameTxt").val(tableName);
        $("#pptFlgChk")[0].checked = pptFlg === "true" ? true : false;
        $("#pptSchemaTxt").val(pptTableSchema);
        $("#pptTableNameTxt").val(pptTableName);
        $("#hisFlgChk")[0].checked = hisFlg === "true" ? true : false;
        $("#hisSchemaTxt").val(hisTableSchema);
        $("#hisTableNameTxt").val(hisTableName);
    });

    $("#addConfigBtn").click(function () {

        var tableName = $("#tableNameTxt").val();
        var pptFlg = $("#pptFlgChk")[0].checked;
        var pptTableSchema = $("#pptSchemaTxt").val();
        var pptTableName = $("#pptTableNameTxt").val();
        var hisFlg = $("#hisFlgChk")[0].checked;
        var hisTableSchema = $("#hisSchemaTxt").val();
        var hisTableName = $("#hisTableNameTxt").val();

        var hisConfigure = {
            tableName: tableName,
            pptFlg: pptFlg,
            pptTableSchema: pptTableSchema,
            pptTableName: pptTableName,
            hisFlg: hisFlg,
            hisTableSchema: hisTableSchema,
            hisTableName: hisTableName
        };

        $.ajax({
            type: "POST",
            url: "addConfig.do",
            async: false,
            dataType: 'json',
            data: hisConfigure,
            success: function (config) {
                console.info(config);
                // showMessengerSuccessDialog("新增配置成功", 2);
                $("#queryBtn").click();
            },
            error: function (e, f) {
                console.info(e);
                console.info(f);
            }
        });

        return false;
    });
});
